//
//  PKReadMainViewController.m
//  PianKe
//
//  Created by lanou on 15/12/21.
//  Copyright © 2015年 KFC. All rights reserved.
//

#import "PKReadMainViewController.h"
#import "PKReadCollectionViewCell.h"
#import "PKReadCollectionReusableView.h"
#import "PKReadModel.h"
#import "PKReadDetailViewController.h"
#import "PKReadWebViewController.h"


@interface PKReadMainViewController () <UICollectionViewDataSource,UICollectionViewDelegate,SDCycleScrollViewDelegate>

// 数据源
@property (strong, nonatomic) NSMutableArray *dataArray;
// 轮播图数据源
@property (strong, nonatomic) NSMutableArray *carouselArray;

@property (strong, nonatomic) UICollectionView *collectView;


@end

@implementation PKReadMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
 
    [self addCollectView];
    
    [self setUpData];
}


// 处理数据
- (void)setUpData
{
    
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = @"XMdnEiW0m3qxDCMVzGMTikDJxQ8aoNbKF8W1rUDRicWP23tB NQhpd6fw";
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
    parDic[@"version"] = @"3.0.2";
    
    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/read/columns" parDic:parDic Method:POST finish:^(NSData *data) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSDictionary *dataDic = dic[@"data"];
        NSArray *listArray = dataDic[@"list"];
       
        // 初始化数据源数组
        _dataArray = [NSMutableArray array];
        for (NSDictionary *oneDic in listArray) {
            
            PKReadModel *model = [[PKReadModel alloc] init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_dataArray addObject:model];
            
        }
        
        NSArray *carousel = dataDic[@"carousel"];
        // 初始化数据源数组
        _carouselArray = [NSMutableArray array];
        for (NSDictionary *twoDic in carousel) {
            
            PKReadModel *model = [[PKReadModel alloc] init];
            [model setValuesForKeysWithDictionary:twoDic];
            [_carouselArray addObject:model];
            
        }
        
        // 刷新
        [_collectView reloadData];
        
        
    } error:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
}


// 添加collect
- (void)addCollectView
{
    
    // 创建一个网状格局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置属性
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.itemSize = CGSizeMake((kScreenWidth - 40) / 3, (kScreenWidth - 40) / 3);
    // 设置滑动的方法
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 设置section的内编剧
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    // 设置表头 和 表尾 的高度
    layout.headerReferenceSize = CGSizeMake(kScreenWidth, 250*kHeightBase);
    layout.footerReferenceSize = CGSizeMake(kScreenWidth, 160*kHeightBase);
    
    _collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) collectionViewLayout:layout];
    _collectView.backgroundColor = [UIColor whiteColor];
    // 设置代理和数据源
    _collectView.delegate = self;
    _collectView.dataSource = self;
    
    
    // 注册cell 和 表头
    [_collectView registerClass:[PKReadCollectionViewCell class] forCellWithReuseIdentifier:@"PKMyCell"];
    [_collectView registerClass:[PKReadCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
    // 第一句: 如果图片大于cell 大小 那么决定图片显示的方式 或者说位置
    // 第二句: 减去超出cell 的部分
    _collectView.contentMode = UIViewContentModeScaleAspectFill;
    _collectView.layer.masksToBounds = YES;
    
    
    [self.view addSubview:_collectView];
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _dataArray.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PKReadCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PKMyCell" forIndexPath:indexPath];
    // 展示数据
    cell.model = _dataArray[indexPath.row];
    
    return cell;
}


// 返回表头 和 表尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
        
        // 表头
        PKReadCollectionReusableView *headV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
        headV.backgroundColor = [UIColor magentaColor];
        
        NSMutableArray *urlArray = [NSMutableArray array];
        for (PKReadModel *model in _carouselArray) {
            
            [urlArray addObject:model.img];
            
        }
       
        SDCycleScrollView *scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, 200*kHeightBase) imageURLStringsGroup:urlArray];
        scrollView.currentPageDotColor = [UIColor yellowColor];
        scrollView.pageDotColor = [UIColor whiteColor];
        scrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        scrollView.autoScrollTimeInterval = 5;
        scrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        scrollView.delegate = self;
        [headV addSubview:scrollView];
    
    UIView *cateView = [[UIView alloc] initWithFrame:CGRectMake(0, scrollView.bottom, kScreenWidth, 50*kHeightBase)];
    cateView.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10*kWidthBase, 0, cateView.width/2, cateView.height)];
    titleLabel.text = @"分类·Categories";
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.font = [UIFont systemFontOfSize:16];
    [cateView addSubview:titleLabel];
    
    UIButton *ALLButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    ALLButton.frame = CGRectMake(cateView.width - 50*kWidthBase, titleLabel.top, 50*kWidthBase, titleLabel.height);
    [ALLButton setTitle:@"ALL >" forState:(UIControlStateNormal)];
    [ALLButton setTitleColor:[UIColor greenColor] forState:(UIControlStateNormal)];
    ALLButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [cateView addSubview:ALLButton];
   // [ALLButton addTarget:self action:@selector(ALLButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    [headV addSubview:cateView];
    

        
        return headV;
    
    
    
}


// 点击cell的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    PKReadDetailViewController *DTVC = [[PKReadDetailViewController alloc] init];
    PKReadModel *model = _dataArray[indexPath.row];
    DTVC.myTypeId = [NSString stringWithFormat:@"%ld",(long)model.type];
    NSLog(@"%@",DTVC.myTypeId);
    
    [self.navigationController pushViewController:DTVC animated:YES];
    
    
}


// scrollView的代理方法
// 点击轮播图,实现的方法
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    
    NSLog(@"%ld",(long)index);
   
    PKReadModel *model = _carouselArray[index];
    PKReadWebViewController *webVC = [[PKReadWebViewController alloc] init];
    NSString *portURL = [model.url substringFromIndex:17];
    webVC.url = portURL;
    [self.navigationController pushViewController:webVC animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
